﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using SpaceGame.Objects;

namespace SpaceGame.Engine
{
    class SpaceGameEngine
    {
        public Timer Timer = new Timer();
        public List<Player>  Players = new List<Player>();
        public List<Star>  Stars = new List<Star>();
        public List<Asteroid>  Asteroids = new List<Asteroid>();
        
        Random r = new Random();
        public void The_art_is_an_explosion()
        {
        
        }
        public SpaceGameEngine()
        {
            Player player1 = new Player();
            Player player2 = new Player();
            Players.Add(player1);
            Players.Add(player2);
            player1.X = -300;
            player2.X = 300;
            player1.Y = -100;
            player2.Y = -100;
            player1.Life = 10000;
            player2.Life = 10000;
       
        }

        // Начало игры
        public void StartGame()
        {
            //oni poyavilisa
            Timer.Start();
            Timer.Interval = 5;
            Timer.Elapsed += (sender, args) => Process();           
        }

        public void Process()
        {
            // Создаем астероиды
            if (r.Next(1, 75) == 34)
            {
                Asteroid asteroid = new Asteroid();
                asteroid.Z = 1000;
                asteroid.FinalX = r.Next(-500,500);
                asteroid.FinalY = r.Next(-500,500);
                asteroid.Speed = (int)((double)asteroid.Z * (double)r.Next(5, 10) / Timer.Interval);
                Asteroids.Add(asteroid);
             

            }
            // Двигаем астероиды
            foreach(Asteroid asteroid  in Asteroids )
            {
                if (asteroid.Z != 0)
                {

              
                asteroid.X = asteroid.FinalX * 100 / asteroid.Z;
                asteroid.Y = asteroid.FinalY * 100 / asteroid.Z;
                asteroid.Z = asteroid.Z - asteroid.Speed;
                }
            }

            // Обрабатываем столкновения

            for (int i =0; i < Players.Count; i++)
            {
                Player player1 = Players[i];
                for (int j = i + 1; j < Players.Count; j++)
                {
                    Player player2 = Players[j];

                    if (player1.X + player1.Image.Width / 2 <= player2.X + player2.Image.Width/2)//  player.X - player.Image.Width/2)
                    {
                        if (player1.X + player1.Image.Width / 2 >= player2.X - player2.Image.Width/2)
                        {
                            if (player1.Y + player1.Image.Height / 2 <= player2.Y + player2.Image.Height/2)//  player.X - player.Image.Width/2)
                            {
                                if (player1.Y + player1.Image.Height / 2 >= player2.Y- player2.Image.Height/2)
                                {
                                    player1.Life = player1.Life - 2500;
                                    player2.Life = player2.Life - 2500;
                                 
                                            player1.X = player1.X - 50;
                                            player2.X = player2.X + 50;

                                            player1.Y = player1.Y - 50;
                                            player2.Y = player2.Y + 50;
                                       
                                    
                                }
                            }
                        }
                    }
                }
 
            }
          }

        
        public void ProcessKeyPressed(string key)
        {
            foreach (var player in Players)
            {
                player.DoActionOnKeyPress(key);
            }
        }

}

}

